package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC385 {
    public long startExecuteTime = System.currentTimeMillis();


    // This is the interface that allows for creating nested lists.
    // You should not implement it, or speculate about its implementation
    public class NestedInteger {
        // Constructor initializes an empty nested list.
        public NestedInteger() {

        }

        // Constructor initializes a single integer.
        public NestedInteger(int value) {

        }

        // @return true if this NestedInteger holds a single integer, rather than a nested list.
        public boolean isInteger() {
            return false;
        }

        // @return the single integer that this NestedInteger holds, if it holds a single integer
        // Return null if this NestedInteger holds a nested list
        public Integer getInteger() {
            return 1;
        }

        // Set this NestedInteger to hold a single integer.
        public void setInteger(int value) {
        }

        // Set this NestedInteger to hold a nested list and adds a nested integer to it.
        public void add(NestedInteger ni) {
        }

        // @return the nested list that this NestedInteger holds, if it holds a nested list
        // Return null if this NestedInteger holds a single integer
        public List<NestedInteger> getList() {
            return null;
        }
    }

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 首先题意不明所以，其次dfs确实不好写
     * @return:
     */
    class Solution {
        //递归函数通过字符数组和cur下标确定要处理的位置
        char[] charArray;
        int cur = 0;

        public NestedInteger deserialize(String s) {
            charArray = s.toCharArray();
            //本身不是一个集合而是一个整数的情况
            if (charArray[0] != '[') return new NestedInteger(Integer.valueOf(s));
            //调用递归函数返回根集合
            return getNest();
        }

        public NestedInteger getNest() {
            NestedInteger nest = new NestedInteger();
            int num = 0;//num用于缓存用逗号分割的整数类型的值
            int sign = 1;//当前记录的整数的符号，1代表整数，-1代表负数
            while (cur != charArray.length - 1) {
                cur++;
                if (charArray[cur] == ',') continue;
                if (charArray[cur] == '[') nest.add(getNest());//遇到[递归获取子集合
                else if (charArray[cur] == ']') return nest;
                else if (charArray[cur] == '-') sign = -1;
                else {//是数字的情况
                    num = 10 * num + sign * (charArray[cur] - '0');
                    //如果下一个字符是,或者]说明当前数字已经记录完了，需要加入集合中
                    if (charArray[cur + 1] == ',' || charArray[cur + 1] == ']') {
                        nest.add(new NestedInteger(num));
                        num = 0;
                        sign = 1;
                    }
                }
            }
            return null;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC385 an = new LC385();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
